#include "debug_renderer.hpp"


void DebugRenderer::renderSkeleton(aer::Camera *pCamera, const Skeleton *pSkeleton)
{
  if (!program.isGenerated())
  {
    program.generate();
      program.addShader( aer::Program::VERTEX_SHADER, "DispAxis.VS" );
      program.addShader( aer::Program::GEOMETRY_SHADER, "DispAxis.GS" );
      program.addShader( aer::Program::FRAGMENT_SHADER, "DispAxis.FS" );
    AER_CHECK( program.link() );
  }
  
  const glm::mat4 &viewProj = pCamera->getViewProjectionMatrix();
  
  glDisable( GL_DEPTH_TEST );  
  program.bind();  
        
  for (int i=0; i<pSkeleton->m_jointCount; ++i)
  {
#   if 0
    // the default bind matrix [works]
    // The global matrix is the inverse of the InvBindMatrix
    const glm::mat4 model = pSkeleton->m_dbgGlobalBind[i];
#   else
    // the current animation pose matrix [does not work]
    const glm::mat4 model = pSkeleton->m_animGlobalPos[i];
#   endif

    program.setUniform( "uMVP", viewProj*model);
    
    glm::vec4 v = glm::vec4( 0.0f, 0.0f, 0.0f, 1.0f);
    glVertexAttrib4fv( 0, glm::value_ptr(v));
    
    glDrawArrays( GL_POINTS, 0, 1);
  }  
  program.unbind();  
  glEnable( GL_DEPTH_TEST );
  
  
  CHECKGLERROR();
}
